package com.sihuatech.coreagent.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;
import com.sihuatech.coreagent.controller.CodeRelationshipController;
import com.sihuatech.coreagent.core.communication.entity.CodeRelationship;
import com.sihuatech.coreagent.core.communication.entity.Response;
import com.sihuatech.coreagent.mapper.CodeRelationshipMapper;
import com.sihuatech.coreagent.redis.RedisManager;
import com.sihuatech.coreagent.until.Consts;

@Service
public class CodeRelationshipService {
	
	private static final Logger logger = LoggerFactory.getLogger(CodeRelationshipService.class);
	@Autowired
	private CodeRelationshipMapper codeRelationshipMapper; 
	@Autowired
	private RedisManager redisManager;
 
	public Response saveCodeRelationship(CodeRelationship codeRelationship,Response res) throws Exception {
		//必填项校验
		if(!checkCodeRelationship(codeRelationship)) {
			res.failWithMessage("缺少必填项");
			return res;
		}
		boolean add = true;
		if(codeRelationship.getId() != null) {
			add = false ; 
		}
		if(!checkCode(codeRelationship,add)) {
			res.failWithMessage("同平台省份编码不可重复,编码为" +codeRelationship.getPlatformProvinceCode());
			return res;
		} 
		
		//入库保存
		try {
			String oldKey = "";
			if(codeRelationship.getId() == null) {
				codeRelationshipMapper.insert(codeRelationship);
			}else {
				CodeRelationship cr = codeRelationshipMapper.selectById(codeRelationship.getId());
				oldKey = cr.getPlatformProvinceCode() + cr.getPlatformType();
				codeRelationshipMapper.updateById(codeRelationship);
			}
			logger.info("省份映射关系入库成功：" + codeRelationship.toString());
			res.success();
			redisManager.addCodeRelationship(codeRelationship.getPlatformProvinceCode(),codeRelationship.getPlatformType(), codeRelationship,oldKey);
		}catch (Exception e) {
			logger.info("入库异常",e);
			res.failWithMessage("入库异常,失败");
		}

		return res;
	}
	
	public boolean checkCodeRelationship(CodeRelationship codeRelationship) {
		return true;
	}
	
	public boolean checkCode(CodeRelationship codeRelationship, boolean add) throws Exception {
		// 逻辑 : add 参数区分 新增修改 ;
		// 新增 平台与平台编码查数据库，有则校验结果为失败，反之成功;
		// 修改 根据ID取修改前的数据OLD，根据两个字段查数据库 无则成功，有则对比两个字段 一致时 对比old new 省份字段 一致时 成功 反之失败。
		Map<String, Object> columnMap = Maps.newHashMap();
		columnMap.put("platform_province_code", codeRelationship.getPlatformProvinceCode());
		columnMap.put("platform_type", codeRelationship.getPlatformType());
		// 为空时为[] 需判断size确定是否有数据
		List<CodeRelationship> list = codeRelationshipMapper.selectByMap(columnMap);
		if (list.size() == 0) {
			return true;
		}
		if (add) {
			return false;
		}
		CodeRelationship cr = codeRelationshipMapper.selectById(codeRelationship.getId());
		// 理论上不为空
		if (cr == null) {
			throw new Exception();
		}
		if (StringUtils.equals(cr.toString(), list.get(0).toString())) {
			return true;
		}
		return false;
	}
	
	public boolean deleteByIds(String[] ids) {
		try {
			for (String id : ids) {
				CodeRelationship codeRelationship = codeRelationshipMapper.selectById(id);
				logger.info("删除id为" + id + ",的省份编码关系," + "具体信息为" + codeRelationship.toString());
				codeRelationshipMapper.deleteById(id);
				//删除数据库中后删redis中的
				redisManager.deleteInfo(codeRelationship.getPlatformProvinceCode() + codeRelationship.getPlatformType());
			}
		}catch (Exception e) {
			logger.info("删除异常");
			return false;
		}
		return true;
	}
}
